package com.jy.modules.cims.component.image.service;

import java.util.List;
import java.util.Observable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.component.image.TransferImagesDao;
import com.jy.modules.cims.component.image.TransferImagesDto;
import com.jy.modules.cims.component.image.TransferImagesHelper;
import com.jy.modules.cims.component.image.TransferImagesQueue;
import com.jy.modules.cims.data.dto.ImgurlDto;
import com.jy.modules.cims.sysUtils.ImgurFtpConfigInfo;

/**
 * @description 下载服务
 * @author shengzhoutao
 * @date 2015-09-29
 */
@Component
public class DownImagesFromUrlService extends Observable {
	String FTP_COMPRESSED_YES = "Y";// 是否压缩文件上传（Y：压缩；N：不压缩）
	
	private static Logger logger = LoggerFactory.getLogger(DownImagesFromUrlService.class);

	@Autowired
	private SpringContextUtils springContextUtils;

	/**
	 * @description
	 * @param transferImagesDto
	 */
	public void doSomething(TransferImagesDto transferImagesDto) {
		ImgurFtpConfigInfo imgurFtpConfigInfo = null;
		boolean downloadSuccess = false;
		String cooperorgCode = transferImagesDto.getCooperorgCode();
		String contractNo = transferImagesDto.getContractNo();
		
		TransferImagesHelper transferImagesHelper = springContextUtils.getBean(TransferImagesHelper.class);
		try {
			TransferImagesDao transferImagesDao = springContextUtils.getBean(TransferImagesDao.class);
			List<ImgurlDto> imgurlDtoList = transferImagesDao.getImgurlByContractNo(contractNo);
			if (CollectionsUtils.isEmpty(imgurlDtoList)) {
				return;
			}

			// 根据合作机构编码获取配置文件信息
			imgurFtpConfigInfo = transferImagesHelper.getImgurFtpConfigInfoByCooperorgCode(cooperorgCode);
			if(null == imgurFtpConfigInfo){
				return;
			}

			// 创建临时文件目录
			transferImagesHelper.createTempDir(imgurFtpConfigInfo, transferImagesDto);
			// 下载目录
			downloadSuccess = transferImagesHelper.downloadURLImgForMoreThread(imgurFtpConfigInfo, transferImagesDto, imgurlDtoList);

			// 一个合同所有的影像文件下载成功，加入下一个队列。
			if (downloadSuccess) {
				logger.info("下载成功的合同号:" + transferImagesDto.getContractNo());
				String ftpCompressed = imgurFtpConfigInfo.getFtpCompressed();
				// 如果上传需要压缩包影像文件，需要放到压缩队列
				if (FTP_COMPRESSED_YES.equalsIgnoreCase(ftpCompressed)) {
					// 队列放数据
					TransferImagesQueue.zipQueue.offer(transferImagesDto);
				}
				// 如果不需要压缩，直接放到上传队列
				else {
					// 队列放数据
					TransferImagesQueue.uploadQueue.offer(transferImagesDto);
				}
			}//
		} catch (Exception e) {
			logger.error(CimsExceptionConstant.TRANSFER_IMG_FAIL, e);
			downloadSuccess = false;
		} finally {
			// 下载失败，清理队列数据和本地文件
			if (!downloadSuccess) {
				transferImagesHelper.delDealData(imgurFtpConfigInfo, transferImagesDto);
			}
		}
		// //
	}
}
